package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Jiedairen;
import com.pojo.Zixun;
import com.service.JiedairenService;
import com.service.ZixunService;
import com.util.MD5Util;
import com.util.PasswordUtil;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 客服管理
 * @author Liao
 */
@RestController
@RequestMapping("/kefu")
public class JiedairenAction {
    @Autowired
    JiedairenService jiedairenService;

    /**
     * 客服登录
     * @param jiedairen
     * @param request
     * @return
     */
    @PostMapping("/kefuLogin")
    public ResponseBean kefuLogin(@RequestBody Jiedairen jiedairen,HttpServletRequest request) {
        if (StringUtil.isNull(jiedairen.getJdphone()) || StringUtil.isNull(jiedairen.getJdpassword())) {
            return ResponseBean.error("客服名或者密码不能为空");
        }

        Jiedairen kefu = jiedairenService.getOne(new QueryWrapper<Jiedairen>().eq("jdphone", jiedairen.getJdphone()).eq("jdpassword", MD5Util.getMD5(jiedairen.getJdpassword())));
        if (kefu != null) {
            if (kefu.getStatus() == 0) {
                return new ResponseBean(500, "该客服已被禁用", kefu);
            } else {
                request.getSession().setAttribute("kefu", kefu);
                return new ResponseBean(200, "登录成功", kefu);
            }
        } else {
            return new ResponseBean(500, "客服名或密码错误", kefu);
        }
    }
    //判断是否客服登录
    @ResponseBody
    @RequestMapping("/pandeng_kefu")
    public ResponseBean pandeng_kefu(HttpServletRequest request) {
        Jiedairen kefu = (Jiedairen) request.getSession().getAttribute("kefu");
        if ( kefu!= null) {
            return new ResponseBean(0,"登录成功",kefu.getJdname());
        }
        return new ResponseBean(500,"登录失败","登录失败");
    }
    /**
     * 分页查询客服
     * @param page
     * @param limit
     * @param kefuName
     * @param status
     * @return
     */
    @RequestMapping("/selectKefu")
    public ResponseBean selectKefu(int page, int limit, String kefuName, @RequestParam(defaultValue = "-1") int status){
        IPage iPage =jiedairenService .getKefuPage(page, limit, kefuName, status);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

    /**
     * 新增客服
     * @param kefu
     * @return
     */
    @PostMapping("/addKefu")
    public ResponseBean addKefu(@RequestBody Jiedairen kefu) {
        if (StringUtil.isNull(kefu.getJdname()) || StringUtil.isNull(kefu.getJdpassword())) {
            return new ResponseBean(500, "用户名和密码不能为空", null);
        }
        if (jiedairenService.getOne(new QueryWrapper<Jiedairen>().eq("jdphone", kefu.getJdphone())) != null) {
            return new ResponseBean(500,"手机号码重复！",kefu);
        }
        String pwd = MD5Util.getMD5(kefu.getJdpassword());
        kefu.setJdpassword(pwd);
        if (jiedairenService.save(kefu)) {
            return new ResponseBean(200, "新增成功", kefu);
        } else {
            return new ResponseBean(500, "新增失败", kefu);
        }
    }
    /**
     * 修改客服的状态
     *
     * @param id     客服id
     * @param status 状态 0或者1
     * @return
     */
    @RequestMapping("/updateStatus")
    public ResponseBean updateStatus(String id, Integer status) {
        Jiedairen kefu = jiedairenService.getById(id);
        if (kefu == null) {
            return new ResponseBean(500, "未找到客服", id);
        }
        kefu.setStatus(status);
        if (jiedairenService.updateById(kefu)) {
            return new ResponseBean(200, "修改成功", kefu);
        } else {
            return new ResponseBean(500, "修改失败", kefu);
        }
    }
    /**
     * 修改客服名或密码
     *
     * @param id        客服id
     * @param columName 修改的字段
     * @param value     修改的值
     * @return
     */
    @RequestMapping("/edit")
    public ResponseBean edit(String id, String columName, String value) {
        String s = jiedairenService.edit(id, columName, value);
        if (s == null) {
            return new ResponseBean(200, "修改成功", null);
        } else {
            return new ResponseBean(500, "修改失败", s);
        }
    }
    /**
     * 退出客服登录，后台
     * @param request
     * @return
     */
    @RequestMapping("/quitLogin")
    public ResponseBean quitLogin(HttpServletRequest request){
        try{
            request.getSession().removeAttribute("kefu");
            return new ResponseBean(200,"退出成功",null);
        }catch (Exception e){
            return new ResponseBean(500,"系统异常",null);
        }

    }
    /**
     * 学校修改密码
     * @param oldPwd
     * @param newPwd
     * @param request
     * @return
     */
    @RequestMapping("wy_update_password")
    public ResponseBean updatePassword(String oldPwd , String newPwd , HttpServletRequest request){
        Jiedairen jiedairen = (Jiedairen) request.getSession().getAttribute("kefu") ;

        if(StringUtil.isNull(oldPwd)) {
            return ResponseBean.error("请输入旧密码") ;
        }
        if(StringUtil.isNull(newPwd)) {
            return ResponseBean.error("请输入新密码") ;
        }

        String res = jiedairenService.wyUpdatePassword(oldPwd , newPwd , jiedairen.getJdid()) ;

        return res == null ? ResponseBean.OK : ResponseBean.error(res) ;
    }

}
